<?php
class Modelroadbook extends ModelBase {
	protected $tablename = 'view_roadbook';
	/**
	 *
	 * @var CDbConnection
	 */
	function __construct() {
		parent::__construct ();
	}
	
	// 得到路书列表
	public function getRoadbook($page,$wherearr=0) {
		$blimit = $page * PERPAGE;
		$arr = array ();
		$arr = parent::getTable($this->tablename, $wherearr,$blimit,PERPAGE,' order by update_time desc');
		foreach($arr as $key=>$value){
			if($value['depart'] && $value['destination']){
				$depart = load_model('way')->gettable(array('way_id'=>$value['depart']));
				$destination = load_model('way')->gettable(array('way_id'=>$value['destination']));
				
				$arr[$key]['departname']=$depart[0]['name'];
				$arr[$key]['destinationname']=$destination[0]['name'];
			}else{
				$arr[$key]['departname']='';
				$arr[$key]['destinationname']='';
			}
		}
		
		
		return $arr;
	}
	
	// 到路书列表
	function getTable($where) {
		$arr = parent::getTable ( $this->tablename, $where );
		foreach($arr as $key=>$value){
			if($value['depart'] && $value['destination']){
				$depart = load_model('way')->gettable(array('way_id'=>$value['depart']));
				$destination = load_model('way')->gettable(array('way_id'=>$value['destination']));
				
				$arr[$key]['departname']=$depart[0]['name'];
				$arr[$key]['destinationname']=$destination[0]['name'];
			}
		}
		
		
		return $arr;
	}
	
	// 取路书数量
	function getRcount($wherearr=0) {
		return parent::getRcount ( $this->tablename,$wherearr );
	}
	// 除路书
	/* function delTable($where) {
		return parent::deleteTable ( $this->tablename, $where );
	
	} */
	
	function delTable($wherearr){
		$addarr['action'] = 'delete';
		$addarr['update_time']=time();
		return parent::updateTable($this->tablename, $addarr, $wherearr) ;
	}
	
	// 加或修改路书
	function replaceTable($updatear) {
		$updatear ['data'] ['update_time'] = time ();
		return parent::replaceTable ( $this->tablename, $updatear ['data'] );
	}
	
	function updateTable($addarr,$wherearr){
		$addarr['action'] = 'update';
		$addarr['update_time']=time();
		return parent::updateTable($this->tablename, $addarr, $wherearr) ;
	}

}
?>
